package com.example.qxfw.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JsonUtils {
    /**
     * 文件转换位流
     * @param file 文件路径
     * @return
     * @throws IOException
     */
    public static String readJSONFile(File file) throws IOException {
        StringBuilder jsonString = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                jsonString.append(line);
            }
        }
        return jsonString.toString();
    }

    public static List<List<List<double[]>>> parsePolygonCoordinates(JSONArray coordinates) {
        List<List<List<double[]>>> polygons = new ArrayList<>();
        List<List<double[]>> polygon = new ArrayList<>();
        for (int i = 0; i < coordinates.size(); i++) {
            JSONArray ring = coordinates.getJSONArray(i);
            List<double[]> points = new ArrayList<>();
            for (int j = 0; j < ring.size(); j++) {
                JSONArray point = ring.getJSONArray(j);
                double longitude = point.getDoubleValue(0);
                double latitude = point.getDoubleValue(1);
                points.add(new double[]{longitude, latitude});
            }
            polygon.add(points);
        }
        polygons.add(polygon);
        return polygons;
    }

    public static int countCoordinates(List<List<List<double[]>>> allCoordinates) {
        int count = 0;
        for (List<List<double[]>> coordinates : allCoordinates) {
            for (List<double[]> ring : coordinates) {
                count += ring.size();
            }
        }
        return count;
    }

    public static double[] getMinLatitudeLongitude(List<List<List<double[]>>> allCoordinates) {
        double minLatitude = Double.MAX_VALUE;
        double minLongitude = Double.MAX_VALUE;

        for (List<List<double[]>> coordinates : allCoordinates) {
            for (List<double[]> ring : coordinates) {
                for (double[] point : ring) {
                    double latitude = point[1];
                    double longitude = point[0];
                    minLatitude = Math.min(minLatitude, latitude);
                    minLongitude = Math.min(minLongitude, longitude);
                }
            }
        }

        return new double[]{minLongitude, minLatitude};
    }

    public static double[] getMaxLatitudeLongitude(List<List<List<double[]>>> allCoordinates) {
        double maxLatitude = Double.MIN_VALUE;
        double maxLongitude = Double.MIN_VALUE;

        for (List<List<double[]>> coordinates : allCoordinates) {
            for (List<double[]> ring : coordinates) {
                for (double[] point : ring) {
                    double latitude = point[1];
                    double longitude = point[0];

                    maxLatitude = Math.max(maxLatitude, latitude);
                    maxLongitude = Math.max(maxLongitude, longitude);
                }
            }
        }

        return new double[]{maxLongitude, maxLatitude};
    }

    public static String readJsonFromFile(String filename) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            StringBuilder jsonString = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                jsonString.append(line);
            }
            return jsonString.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void addCoordinatesToScope(JSONArray coordinates, List<List<Double>> scope) {
        for (int i = 0; i < coordinates.size(); i++) {
            JSONArray polygon = coordinates.getJSONArray(i);
            for (int j = 0; j < polygon.size(); j++) {
                JSONArray ring = polygon.getJSONArray(j);
                List<Double> point = new ArrayList<>();
                for (int k = 0; k < ring.size(); k++) {
                    JSONArray coordinate = ring.getJSONArray(k);
                    double longitude = coordinate.getDoubleValue(0);
                    double latitude = coordinate.getDoubleValue(1);
                    point.add(longitude);
                    point.add(latitude);
                }
                scope.add(point);
            }
        }
    }
    public static List<List<Double>> parseJsonToScope(String jsonString) {
        List<List<Double>> scope = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        JSONArray features = jsonObject.getJSONArray("features");
        for (int i = 0; i < features.size(); i++) {
            JSONObject feature = features.getJSONObject(i);
            JSONObject geometry = feature.getJSONObject("geometry");
            JSONArray coordinates = geometry.getJSONArray("coordinates");
            if ("MultiPolygon".equals(geometry.getString("type"))) {
                JSONArray polygonCoordinates = coordinates.getJSONArray(0).getJSONArray(0);
                addCoordinatesToScope(scope, polygonCoordinates);
            }
        }
        return scope;
    }

    private static void addCoordinatesToScope(List<List<Double>> scope, JSONArray coordinates) {
        for (int i = 0; i < coordinates.size(); i++) {
            JSONArray point = coordinates.getJSONArray(i);
            double lon = point.getDouble(0);
            double lat = point.getDouble(1);
            List<Double> coordinate = new ArrayList<>();
            coordinate.add(lon);
            coordinate.add(lat);
            scope.add(coordinate);
        }
    }

}
